package com.yzt.zhuju.download;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * author : bob
 * date   : 2022/11/30
 * desc   :
 */
public class FileProgressInterceptor implements Interceptor {

    private final ProgressListener progressListener;

    public FileProgressInterceptor(ProgressListener progressListener) {
        this.progressListener = progressListener;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        progressListener.onStartDownload(response.body().contentLength());
        return response.newBuilder()
                .body(new ProgressResponseBody(response.body(), progressListener))
                .build();
    }
}